package menu;
import java.awt.BorderLayout;
import java.awt.Color;
import java.io.Console;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;

import chesskers.Driver;
import chesskers.ImagePanel;
import chesskers.UIManager;
import state.Profile;

public class PlayerSelectFrame extends MenuWindow{
	
	ImagePanel panel_mainmenu = new ImagePanel(new ImageIcon("img/box_background.png").getImage());
	JButton button_mm_select = new JButton("Select Profile");
	JButton button_mm_new = new JButton("New Profile");
	JButton button_mm_quit = new JButton("Back");
	ArrayList<Profile> profilesList = new ArrayList<Profile>();
	String profileFileName = "profiles.data";
	DefaultListModel listModel;
	JList list;
	
	public PlayerSelectFrame() {
		super("Chess-kers!");
		
		button_mm_select.setBounds(95, 360, 200, 50);
		button_mm_new.setBounds(305, 360, 200, 50);
		button_mm_quit.setBounds(150, 415, 300, 30);
		
		panel_mainmenu.add(button_mm_select);
		MakeButtonAction(button_mm_select, "select");		
		panel_mainmenu.add(button_mm_new);
		MakeButtonAction(button_mm_new, "new");		
		panel_mainmenu.add(button_mm_quit);
		MakeButtonAction(button_mm_quit, "back");
		
		this.add(panel_mainmenu);
		
		listModel = new DefaultListModel();
		
		/* load profiles */
		
		try{
			FileInputStream fIn = new FileInputStream(profileFileName);
			if(fIn.available() != 0){
				ObjectInputStream in = new ObjectInputStream(fIn);
				profilesList = (ArrayList<Profile>) in.readObject();
				in.close();
				fIn.close();
			}
			else
			{
				fIn.close();
			}
		}
		catch(IOException e){
			JOptionPane.showMessageDialog(this, "Error loading profiles");
			JOptionPane.showMessageDialog(this, e.toString());
		}
		catch(ClassNotFoundException ex){
			JOptionPane.showMessageDialog(this, "Error 2 loading profiles");
		}
		
		/* add model */
		
		for(int i = 0; i < profilesList.size(); i++)
		{
			if (Driver. state == Driver.state.CheckSelect2 || Driver. state == Driver.state.ChessSelect2){
				if (profilesList.get(i).name.equals(Driver.p1.name)){
					profilesList.remove(i);
					break;
				}
			}
		}
		
		for(int i = 0; i < profilesList.size(); i++)
		{
			listModel.addElement(profilesList.get(i).name);
		}
		
		list = new JList(listModel);
		JScrollPane scrollPane = new JScrollPane(list);
		scrollPane.setBounds(175, 80, 250, 280);
		panel_mainmenu.add(scrollPane);
		
		setSize(600, 500);
		setBackground(Color.red);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);
	}
	
	@Override
	public void Action(String cmd) {
		// TODO Auto-generated method stub
		switch (cmd) {
		case "select":
			if (Driver. state == Driver.state.CheckSelect1){
				int selectedIndex = list.getSelectedIndex();
				Driver.p1 = profilesList.get(selectedIndex);
				Driver.state = Driver.state.CheckSelect2;
			}	
			else if (Driver. state == Driver.state.CheckSelect2){
				int selectedIndex = list.getSelectedIndex();
				Driver.p2 = profilesList.get(selectedIndex);
				Driver.state = Driver.state.TimeCheck;
			}
			else if (Driver. state == Driver.state.ChessSelect1){
				int selectedIndex = list.getSelectedIndex();
				Driver.p1 = profilesList.get(selectedIndex);
				Driver.state = Driver.state.ChessSelect2;
			}	
			else if (Driver. state == Driver.state.ChessSelect2){
				int selectedIndex = list.getSelectedIndex();
				Driver.p2 = profilesList.get(selectedIndex);
				Driver.state = Driver.state.TimeChess;
			}
			
			new UIManager().start();
		    dispose();
			break;			
		case "back":
			if (Driver. state == Driver.state.CheckSelect1 || Driver. state == Driver.state.ChessSelect1){
				Driver.state = Driver.state.MainMenu;
			}	
			else if (Driver. state == Driver.state.CheckSelect2){
				Driver.state = Driver.state.CheckSelect1;
			}
			else if (Driver. state == Driver.state.ChessSelect2){
				Driver.state = Driver.state.ChessSelect1;
			}
			new UIManager().start();
		    dispose();
			break;
		case "new":
			String newName = JOptionPane.showInputDialog(this, "Please Input Profile Name",null);
			// open save the new profile name to file
			for(int i = 0; i < profilesList.size(); i++)
			{
				if(newName.equals(profilesList.get(i).name))
				{
					// duplicate name.
					JOptionPane.showMessageDialog(this, "Name already existed");
					break;
				}
			}
			
			FileOutputStream fOut = null;
			ObjectOutputStream objOut = null;			
			
			// add and save
			profilesList.add(new Profile(newName));
			try{
				fOut = new FileOutputStream(profileFileName);
				objOut = new ObjectOutputStream(fOut);
				objOut.writeObject(profilesList);
				objOut.close();
				listModel.addElement(newName);
			}
			catch(IOException e){
				JOptionPane.showMessageDialog(this, "Error saving profiles");
			}
			break;
		default:
			throw new UnsupportedOperationException("Invalid button code: "+cmd);
		}
	}
}